package shuaige.jdk.stream.流基础;

import shuaige.jdk.model.Person;
import shuaige.jdk.函数式接口.取两个值并产生一个新值reduce_BiFunction.BiFunction具体实现;
import shuaige.jdk.函数式接口.取两个值并产生一个新值_类型全部相同reduce_BinaryOperator.BinaryOperator具体实现;

import java.util.*;
import java.util.stream.Stream;

/**
 *
 */
public class 终结流_reduce集合函数 {


    public static void main(String[] args) {
        指定初始值_链接元素1();
        指定初始值_链接元素2();
        指定初始值_链接元素3();
    }

    public static void 无初始值_累加数字1() {
        Optional<Integer> reduce = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce((acc, n) -> acc + n);
        System.out.println(reduce.get());
    }

    public static void 无初始值_累加数字2() {
        Optional<Integer> reduce = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(BinaryOperator具体实现.累加匿名函数);
        System.out.println(reduce.get());
    }

    public static void 指定初始值_累加数字1() {
        int sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(0, (acc, n) -> acc + n);
        System.out.println(sum);
    }

    public static void 指定初始值_累加数字2() {
        int sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(0, Integer::sum);
        System.out.println(sum);
    }

    public static void 指定初始值_累加数字3() {
        int sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9).reduce(0, BinaryOperator具体实现.累加匿名函数);
        System.out.println(sum);
    }

    public static void 指定初始值_转换list为Map() {
        // 按行读取配置文件:
        List<String> props = List.of("profile=native", "debug=true", "logging=warn", "interval=500");
        Map<String, String> map = props.stream()
                // 把k=v转换为Map[k]=v:
                .map(kv -> {
                    String[] ss = kv.split("\\=", 2);
                    return Map.of(ss[0], ss[1]);
                })
                // 把所有Map聚合到一个Map:
                .reduce(new HashMap<>(), (m, kv) -> {
                    m.putAll(kv);
                    return m;
                });
        // 打印结果:
        map.forEach((k, v) -> {
            System.out.println(k + " = " + v);
        });
    }

    public static void 指定初始值_获取最大数字1() {
        List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        // 直接归纳流中的元素，返回一个封装有结果的Optional
        Integer max = ints.stream().reduce(Integer.MIN_VALUE, (integer, integer2) -> {
            if (integer > integer2) {
                return integer;
            } else {
                return integer2;
            }
        });
        System.out.println(max);
    }

    public static void 指定初始值_获取最大数字2() {
        List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        // 直接归纳流中的元素，返回一个封装有结果的Optional
        Integer max = ints.stream().reduce(Integer.MIN_VALUE, Integer::max);
        System.out.println(max);
    }

    public static void 指定初始值_获取最大数字3() {
        List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
        // 直接归纳流中的元素，返回一个封装有结果的Optional
        Integer max = ints.stream().reduce(Integer.MIN_VALUE, BinaryOperator具体实现.获取最大值匿名函数);
        System.out.println(max);
    }

    public static void 指定初始值_链接元素1() {
        List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
        String result = letters
                .stream()
                .reduce("", (partialString, element) -> partialString + element);
        System.out.println(result);
    }

    public static void 指定初始值_链接元素2() {
        List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
        String result = letters.stream().reduce("", String::concat);
        System.out.println(result);
    }

    public static void 指定初始值_链接元素3() {
        List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
        String result = letters.stream().reduce("", BinaryOperator具体实现.链接字符串匿名函数);
        System.out.println(result);
    }

    public static void 有组合函数1() {
        List<Person> users = Arrays.asList(new Person("John", 1), new Person("Julie", 2));
        int result = users.stream()
                .reduce(0, BiFunction具体实现.biFunction, BinaryOperator具体实现.累加匿名函数);
        System.out.println(result);
    }

}
